local header = [[
..\Base.simc

report_details=0

gear_expertise_rating=2551
gear_hit_rating=2551

default_actions=1
]]

local header = [[
../Base.simc

report_details=0

gear_expertise_rating=2551
gear_hit_rating=2551

default_actions=1

html=
reforge_plot_output_file=

gear_agility=31359

main_hand=scaled_weapon,weapon=bow_3.00speed_20531min_38132max,enchant=lord_blastingtons_scope_of_doom

talents=http://cn.battle.net/wow/en/tool/talent-calculator#Ya!002110

]]
local talent_Default = "talents=http://cn.battle.net/wow/en/tool/talent-calculator#Ya!002110"
local talent_Fevor = "talents=http://cn.battle.net/wow/en/tool/talent-calculator#Ya!002010"
local talent_TotH = "talents=http://cn.battle.net/wow/en/tool/talent-calculator#Ya!002210"
local talent_Crows = "talents=http://cn.battle.net/wow/en/tool/talent-calculator#Ya!002100"
local talent_Barrage = "talents=http://cn.battle.net/wow/en/tool/talent-calculator#Ya!002112"
local talent_PS = "talents=http://cn.battle.net/wow/en/tool/talent-calculator#Ya!002111"
local gear_NoTrinket = talent_Default .. "\ntrinket1=haromms_talisman,id=105527,upgrade=2"
local gear_NoTier2 = talent_Default .. "\ntier16_2pc_melee=0\ntier16_4pc_melee=0"
local gear_NoTier4 = talent_Default .. "\ntier16_2pc_melee=1\ntier16_4pc_melee=0"

local Hunter_BM_T16H_info = {
	func = function ( info )
		info.total_agility = info.total_agility or math.floor(5.521437321e-1 * info.total_stat + 12034.63797)
		info.weapon_min = info.weapon_min or math.floor(2.719477338e-1 * info.total_stat + 11013.53165)
		info.weapon_max = info.weapon_max or math.floor(5.050224581e-1 * info.total_stat + 20456.37468)
		info.name_pattern = info.name_pattern or "BM_" .. info.abbrev .. "_%d"
		info.fig_title_pattern = info.fig_title_pattern or "属性收益 - 野兽控制猎人 - " .. info.title_kw .. " - %d"
	end,
	min_stat = 1000,
	delta = 500,
	start_crit = 6000,
	start_haste = 6000,
	baseChar = "Hunter_BM_T16H",
	var_start = 35000,
	var_end = 35000,
	var_step = 500,
	var_name = "total_stat",
	additional_profile = talent_Default,
	iterations = 10000,
}

dofile([[../lib_simc_lua.lua]])

------------------------------------------------------------------------------
function process_session()
	log("Start processing session %s", session.name)
	if fast_mode then
		print("Processing %s", session.name)	-- Fast mode
	end

	title_base = ("title %s"):format(session.name)

	os.execute(title_base)

	session.header = header
	if session.total_agility then
		session.header = session.header .. ("\ngear_agility=%d\n"):format(session.total_agility)
		log("Using agility %d", session.total_agility)
	end

	if session.weapon_dps and session.weapon_range then
		local weapon_range = session.weapon_range / 100
		log("Using weapon dps %d at range %.4f", session.weapon_dps, weapon_range)
		local new_weapon_min = session.weapon_dps * (1 - weapon_range)
		local new_weapon_max = session.weapon_dps * (1 + weapon_range)
		if session.weapon_min ~= new_weapon_min then
			log("Overriding weapon_min %d with %d", session.weapon_min, new_weapon_min)
		end
		if session.weapon_max ~= new_weapon_max then
			log("Overriding weapon_max %d with %d", session.weapon_max, new_weapon_max)
		end
		session.weapon_min = new_weapon_min
		session.weapon_max = new_weapon_max
	end

	if session.weapon_min and session.weapon_max then
		session.header = session.header .. ("\nmain_hand=scaled_weapon,weapon=bow_3.00speed_%dmin_%dmax,enchant=lord_blastingtons_scope_of_doom\n"):format(session.weapon_min, session.weapon_max)
		log("Using weapon damage %d - %d", session.weapon_min, session.weapon_max)
	end

	if session.additional_profile then
		session.header = session.header .. "\n" .. session.additional_profile
	end

	if #session.profiles == 0 then
		for crit = session.min_stat, session.total_stat, session.delta do
			for haste = session.min_stat, session.total_stat, session.delta do
				local mastery = session.total_stat - crit - haste
				if mastery >= session.min_stat then
					local profile = {
						crit = crit,
						haste = haste,
						mastery = mastery,
						profile = ("gear_crit_rating=%d\ngear_haste_rating=%d\ngear_mastery_rating=%d\n"):format(
							crit,
							haste,
							mastery
						)
					}
					-- if session.name == "BM_DB_35000" and import_session then
					-- 	for index, _profile in ipairs(import_session.profiles) do
					-- 		if _profile.crit == crit and _profile.haste == haste and _profile.mastery == mastery then
					-- 			profile = _profile
					-- 			log("Imported #%d(%d, %d, %d)", index, crit, haste, mastery)
					-- 		end
					-- 	end
					-- end

					table.insert(session.profiles, profile)
				end
			end
		end
		save_session(true)
	end

	for index, profile in ipairs(session.profiles) do
		local elapsed, simcElapsed = process_profile(profile, index)
		local estimatedTotal = elapsed * (#session.profiles - index)
		if elapsed > 0 then
			log("Sim #%d took time %.3f / %.3f (%.2f%%), %d sec left.", index, simcElapsed, elapsed, simcElapsed / elapsed * 100, estimatedTotal)
		end
	end

	log("Saving session ...")
	save_session(true)
	log("Finished processing session %s", session.name)
end

function process_base_group(base_info, add_info)
	local combined_info = {}
	for key, value in pairs(base_info) do
		combined_info[key] = value
	end
	for key, value in pairs(add_info) do
		combined_info[key] = value
	end
	for var = combined_info.var_end, combined_info.var_start, -combined_info.var_step do
		local info = {}
		for key, value in pairs(combined_info) do
			info[key] = value
		end
		log("Iterate %s from %s to %s step %s: %s", info.var_name, tostring(info.var_start), tostring(info.var_end), tostring(info.var_step), tostring(var))
		info[info.var_name] = var
		if type(info.func) then
			info.func(info)
		end
		info.name = info.name_pattern:format(var)
		info.fig_title = info.fig_title_pattern:format(var)
		session = {
			profiles = {},
			nodes = {},
		}
		local func = loadfile([[Sessions/]] .. info.name .. [[.lua]])
		if func then
			log("Loading session %s from file ...", info.name)
			func()
		end

		for key, value in pairs(info) do
			if session[key] ~= value then
				log("Warning: Overriding session %s info %s from %s to %s", info.name, key, tostring(session[key]), tostring(value))
			end
			session[key] = value
		end

		process_session()
	end

	log("Generating report ...")

	local csv = io.open([[Sessions\]] .. session.name .. [[.csv]], "w")
	csv:write("Total Stat", "\t")
	csv:write("Crit", "\t")
	csv:write("Haste", "\t")
	csv:write("Mastery", "\t")
	csv:write("DPS", "\t")
	csv:write("Error", "\t")
	csv:write("\n")
	for index, profile in ipairs( session.profiles ) do
		local total_agility = math.floor(5.521437321e-1 * session.total_stat + 12034.63797)
		csv:write(session.total_stat, "\t")
		csv:write(profile.crit, "\t")
		csv:write(profile.haste, "\t")
		csv:write(profile.mastery, "\t")
		csv:write(string.format("%.1f", profile.dps), "\t")
		csv:write(profile.err, "\t")
		csv:write("\n")
	end
	csv:close()
end

-- dofile([[Sessions/Hunter_BM_T16H.lua]])
-- import_session = session
-- session = nil
process_base_group(Hunter_BM_T16H_info, {abbrev = "DB",		title_kw = "基准",												})
process_base_group(Hunter_BM_T16H_info, {abbrev = "Fev",	title_kw = "热情", 			additional_profile = talent_Fevor	})
process_base_group(Hunter_BM_T16H_info, {abbrev = "TotH",	title_kw = "狩猎刺激",		additional_profile = talent_TotH	})
process_base_group(Hunter_BM_T16H_info, {abbrev = "Crow",	title_kw = "夺命黑鸦",		additional_profile = talent_Crows	})
process_base_group(Hunter_BM_T16H_info, {abbrev = "NAoC",	title_kw = "无既定之天命",	additional_profile = gear_NoTrinket	})
process_base_group(Hunter_BM_T16H_info, {abbrev = "4T16",	title_kw = "无4T16",		additional_profile = gear_NoTier4	})
process_base_group(Hunter_BM_T16H_info, {abbrev = "2T16",	title_kw = "无2T16",		additional_profile = gear_NoTier2	})
process_base_group(Hunter_BM_T16H_info, {abbrev = "Barr",	title_kw = "弹幕射击",		additional_profile = talent_Barrage	})
process_base_group(Hunter_BM_T16H_info, {abbrev = "PowS",	title_kw = "强风射击",		additional_profile = talent_PS		})
